import pandas as pd
from sklearn.preprocessing import LabelEncoder

# 读取数据集
data = {
    'age': ['≤30', '≤30', '31~40', '>40', '>40', '>40', '31~40', '≤30', '≤30', '>40', '≤30', '31~40', '31~40', '>40', '≤30'],
    'income': ['high', 'high', 'high', 'medium', 'low', 'low', 'low', 'medium', 'low', 'medium', 'medium', 'medium', 'high', 'medium', 'medium'],
    'student': ['no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes'],
    'credit': ['medium', 'high', 'medium', 'medium', 'medium', 'high', 'high', 'medium', 'medium', 'medium', 'high', 'high', 'medium', 'high', 'medium'],
    'purchase': ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes']
}

df = pd.DataFrame(data)

# 使用LabelEncoder对定性数据进行编码
# LabelEncoder将每个类别映射为一个唯一的整数
le_age = LabelEncoder()
df['age_encoded'] = le_age.fit_transform(df['age'])
# 对应结果：'≤30' -> 0, '31~40' -> 1, '>40' -> 2

le_income = LabelEncoder()
df['income_encoded'] = le_income.fit_transform(df['income'])
# 对应结果：'high' -> 0, 'medium' -> 1, 'low' -> 2

le_student = LabelEncoder()
df['student_encoded'] = le_student.fit_transform(df['student'])
# 对应结果：'no' -> 0, 'yes' -> 1

le_credit = LabelEncoder()
df['credit_encoded'] = le_credit.fit_transform(df['credit'])
# 对应结果：'high' -> 0, 'medium' -> 1

print(df)